package com.app.view;

import android.annotation.SuppressLint;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.util.StateSet;
import android.view.View;

public class ShapeBackgroundHelper {

	// public ShapeBackgroundHelper(View view){
	//
	// }

	private GradientDrawable background;
	private int mStrokeWidth;
	private int mStrokeColor;
	private float topLeftRadius;
	private float topRightRadius;
	private float bottomLeftRadius;
	private float bottomRightRadius;

	private StateListDrawable mStateDrawable;

	/** 边框是否是虚线 */
	private boolean dash;
	private float dashWidth;
	private float dashGap;

	private int colorNormal;
	private int colorPressed;
	private int colorDisabled;

	public void setCornerRadius(float topLeftRadius, float topRightRadius,
			float bottomLeftRadius, float bottomRightRadius) {
		this.topLeftRadius = topLeftRadius;
		this.topRightRadius = topRightRadius;
		this.bottomLeftRadius = bottomLeftRadius;
		this.bottomRightRadius = bottomRightRadius;
	}

	public void setCornerRadius(float radius) {
		topLeftRadius = topRightRadius = bottomLeftRadius = bottomRightRadius = radius;
	}

	public void setStroke(int strokeWidth, int strokeColor) {
		this.mStrokeWidth = strokeWidth;
		this.mStrokeColor = strokeColor;
	}

	/**
	 * @param isdash
	 *            边框是否是虚线
	 * @param dashWidth
	 *            虚线的长度 px
	 * @param dashGap
	 *            虚线间的距离 px
	 */
	public void setDash(boolean isdash, float dashWidth, float dashGap) {
		this.dash = isdash;
		this.dashWidth = dashWidth;
		this.dashGap = dashGap;
	}

	public void setStateListColor(int normalColor, int pressedColor,
			int disabledColor) {
		this.colorNormal = normalColor;
		this.colorPressed = pressedColor;
		this.colorDisabled = disabledColor;
	}

	public void setBackground(View view) {

		initStateDrawable();
		setBackgroundCompat(view);

	}

	private GradientDrawable createDrawable(int color, int strokeColor) {
		GradientDrawable drawable = new GradientDrawable();
		drawable.setColor(color);
		drawable.setCornerRadii(new float[] { topLeftRadius, topLeftRadius,topRightRadius,topRightRadius
				,bottomLeftRadius,bottomLeftRadius ,bottomRightRadius ,bottomRightRadius});
		if (!dash) {
			drawable.setStroke(mStrokeWidth, strokeColor);
		} else {
			drawable.setStroke(mStrokeWidth, strokeColor, dashWidth, dashGap);
		}
		return drawable;
	}

	private void initStateDrawable() {
		if (background == null) {
			background = createDrawable(colorNormal, mStrokeColor);
		}

		GradientDrawable drawableDisabled = createDrawable(colorDisabled,
				mStrokeColor);
		GradientDrawable drawableFocused = createDrawable(colorPressed,
				mStrokeColor);
		GradientDrawable drawablePressed = createDrawable(colorPressed,
				mStrokeColor);
		mStateDrawable = new StateListDrawable();

		mStateDrawable.addState(new int[] { android.R.attr.state_pressed },
				drawablePressed);
		mStateDrawable.addState(new int[] { android.R.attr.state_focused },
				drawableFocused);
		mStateDrawable.addState(new int[] { -android.R.attr.state_enabled },
				drawableDisabled);
		mStateDrawable.addState(StateSet.WILD_CARD, background);
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	private void setBackgroundCompat(View view) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
			view.setBackground(mStateDrawable);
		} else {
			view.setBackgroundDrawable(mStateDrawable);
		}
	}
}
